package com.example.AdminComponent;

import com.example.Utils.HttpUtil;
import com.example.Utils.ResultUtil;
import com.example.pojo.ParseStudent;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;


public class CourseApply extends Box {

    JTable Table;
    Vector<String> course = new Vector<>();
    Vector<Vector<String>> courses = new Vector<Vector<String>>();
    TableModel tm;



    //构造方法
    public CourseApply(){
        super(BoxLayout.Y_AXIS);
        //组装
        JPanel jPanel = new JPanel();
        jPanel.setBackground(new Color(208, 210, 213, 179));
        jPanel.setMaximumSize(new Dimension(700, 50));
        JButton b3=new JButton("查询");
        JButton b4=new JButton("确定");
        JLabel name=new JLabel("课程名");
        JTextField t1=new JTextField(6);
        Box box1= Box.createHorizontalBox();
        jPanel.add(name);
        jPanel.add(t1);
        jPanel.add(b3);
        jPanel.add(Box.createHorizontalStrut(50));
        jPanel.add(b4);
        this.add(jPanel);



        //组装表格
        String []ss={"课程号","课程名","教师","学时","时期","上课时间","上课教室","简介","状态",};
        for (int i = 0; i < ss.length; i++) {
            course.add(ss[i]);
        }
        requestPostData("http://localhost:8080/course");
        tm=new DefaultTableModel(courses,course);
        Table=new JTable(tm){
            @Override
            public boolean isCellEditable(int row,int column){
                return false;
            }
        };

        //只能选中一行
        Table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();//单元格渲染器
        tcr.setHorizontalAlignment(JLabel.CENTER);//居中显示
        Table.setDefaultRenderer(Object.class, tcr);//设置渲染器
        JScrollPane scrollPane=new JScrollPane(Table);
        Table.setFont(new Font("宋体",Font.PLAIN,15));
        this.add(scrollPane);

        //查询按钮绑定
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!t1.getText().trim().equals("")) {
                    requestGetData("http://localhost:8080/selectCourse" + "?name=" + t1.getText());
                }else
                {
                    requestGetData("http://localhost:8080/selectCourse");
                }
                //刷新表格
                Table.updateUI();
            }
        });





        /*
         * 确定按钮
         * */
        b4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = Table.getSelectedRow();//获取行
                Map<String, String> map = new HashMap<>();
                map.put("id", (String) tm.getValueAt(row, 0));
                map.put("name", (String) tm.getValueAt(row, 1));
                map.put("teacher", (String) tm.getValueAt(row, 2));
                map.put("creditHour",(String) tm.getValueAt(row, 3));
                map.put("period",(String) tm.getValueAt(row, 4));
                map.put("classTime", (String) tm.getValueAt(row, 5));
                map.put("classRoom", (String) tm.getValueAt(row, 6));
                map.put("intro",(String) tm.getValueAt(row, 7));
                map.put("state","3");
                HttpUtil.sendPost("http://localhost:8080/course/update", map);
                JOptionPane.showMessageDialog(null, "修改成功！，点击查询更新数据");

            }
        });




    }

    //请求数据
    public  void requestPostData( String url){
        String result=HttpUtil.sendPost(url);
        Vector<com.example.pojo.Course> course= ResultUtil.ResultVectorCourse(result);
        Vector<Vector<String>> ss=new Vector<>();
        for (int i = 0; i < course.size(); i++) {
           if(!course.get(i).getState().equals("3")) {
               Vector<String> s=new Vector<>();
               s.add(course.get(i).getId());
               s.add(course.get(i).getName());
               s.add(course.get(i).getTeacher());
               s.add(course.get(i).getCreditHour());
               s.add(course.get(i).getPeriod());
               s.add(course.get(i).getClassTime());
               s.add(course.get(i).getClassRoom());
               s.add(course.get(i).getIntro());
               s.add(courseState(course.get(i).getState()));
               ss.add(s);
           }
        }

        courses.clear();
        for (Vector<String> sv  : ss) {
            courses.add(sv);
        }
    }



    public  void requestGetData( String url) {
        String result=HttpUtil.sendGet(url);
        Vector<com.example.pojo.Course> course= ResultUtil.ResultVectorCourse(result);
        Vector<Vector<String>> ss=new Vector<>();
        for (int i = 0; i < course.size(); i++) {
            if(!course.get(i).getState().equals("3")) {
                Vector<String> s=new Vector<>();
                s.add(course.get(i).getId());
                s.add(course.get(i).getName());
                s.add(course.get(i).getTeacher());
                s.add(course.get(i).getCreditHour());
                s.add(course.get(i).getPeriod());
                s.add(course.get(i).getClassTime());
                s.add(course.get(i).getClassRoom());
                s.add(course.get(i).getIntro());
                s.add(courseState(course.get(i).getState()));
                ss.add(s);
            }
        }

        courses.clear();
        for (Vector<String> sv  : ss) {
            courses.add(sv);
        }

    }

    public String courseState(String state){
        switch (state){
            case "0"->{return "申请增加新课程";}
            case "1"->{return "申请删除旧课程";}
            case "2"->{return "申请修改课程";}
            case "3"->{return "可选";}
            case "4"->{return "等待课程安排";}
            case "5"->{return "已删除";}
            case "6"->{return "拒绝申请";}
        }
        return null;
    }

}
